package com.tulei.api.crawler;

import com.tulei.system.Entity.Movie;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author TuLei
 * @Date 2024-06-03 10:37
 **/

public class CrawlerData {


    public static List<Movie> getData()  {


        List<Movie> list = new ArrayList<>();

        for (int i = 2; i < 4; i++) {
            list.addAll(extracted(i));
        }

        return list;

    }

    private static List<Movie> extracted(int i) {
        List<Movie> list = new ArrayList<>();
        Connection connect = Jsoup.connect("https://vip.1905.com/list/t_1/p" + i +" o6.shtml");
        try {
            Document document = connect.get();
            Elements elements = document.getElementsByClass("page page_catalog");
            Elements elements1 = elements.get(0).getElementsByClass("sortCon movieList");
            Elements liElements = elements1.get(0).getElementsByTag("li");
            for (Element element : liElements) {
                Movie movie = new Movie();
                Element aElement = element.selectFirst(".img");
                if (aElement == null) {
                    movie.setTitle("");
                    movie.setId("");
                }else {
                    String title = aElement.attr("title");
                    movie.setTitle(title);
                    String dataId = aElement.attr("data-id");
                    movie.setId(dataId);
                }


                Element scoreElement = element.selectFirst(".score");
                if (scoreElement == null) {
                    movie.setScore("");
                }else {
                    String score = scoreElement.text();
                    movie.setScore(score);
                }


                Element yearElement = element.selectFirst(".year");
                if (yearElement == null) {
                    movie.setYear("");
                }else {
                    String year = yearElement.text();
                    movie.setYear(year);
                }


                Element actorsElement = element.selectFirst(".actor");
                if (actorsElement == null) {
                    movie.setActors("");
                }else {
                    String actors = actorsElement.text().trim();
                    movie.setActors(actors);
                }


                Element descriptionElement = element.selectFirst(".descr");
                if (descriptionElement == null) {
                    movie.setDescription("");
                }else {
                    String description = descriptionElement.text().trim();
                    movie.setDescription(description);
                }
                String description = descriptionElement.text().trim();
                movie.setDescription(description);

                Element playUrlElement = element.selectFirst(".txt > a");
                if (playUrlElement == null) {
                    movie.setPlayUrl("");
                }else {
                    String playUrl = playUrlElement.attr("href");
                    movie.setPlayUrl(playUrl);
                }

                list.add(movie);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return list;
    }



}


